package com.fc.v2.service;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.fc.v2.mapper.auto.ScaletabMapper;
import com.fc.v2.model.auto.Scaletab;
import com.fc.v2.model.auto.ScaletabExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.v2.common.base.BaseService;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.mapper.auto.DetectionMapper;
import com.fc.v2.model.auto.Detection;
import com.fc.v2.model.auto.DetectionExample;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.util.SnowflakeIdWorker;
import com.fc.v2.util.StringUtils;

/**
 * 检测信息 DetectionService
 * @Title: DetectionService.java 
 * @Package com.fc.v2.service 
 * @author cjh_自动生成
 * @email ${email}
 * @date 2025-01-29 19:14:18  
 **/
@Service
public class DetectionService implements BaseService<Detection, DetectionExample>{
	@Autowired
	private DetectionMapper detectionMapper;

	@Autowired
	private ScaletabMapper scaletabMapper;
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Detection> listnotIn(Tablepar tablepar,Detection detection){

	        DetectionExample testExample=new DetectionExample();
		    DetectionExample.Criteria criteria = testExample.createCriteria();
			//搜索
			if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
	        	testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
	        }else {//大搜索
	        	testExample.createCriteria().andLikeQuery(detection);
	        }
		   //testExample.createCriteria().addCriterion("id NOT IN (SELECT id FROM table1)");
			//表格排序
			if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        }else{
	        	testExample.setOrderByClause("createTime desc");
	        }

		 // 查询 t_scaletab 表中 read2 不为 null 且去重后的 read2 值列表
		 ScaletabExample scaletabExample = new ScaletabExample();
		 scaletabExample.setDistinct(true);
		 scaletabExample.createCriteria().andRead2IsNotNull();
		 List<Scaletab> scaletabs = scaletabMapper.selectByExample(scaletabExample);

		 // 提取 read2 值到一个列表中
		 java.util.List<String> read2List = new java.util.ArrayList<>();
		 for (Scaletab scaletab : scaletabs) {
			 if (scaletab.getRead2() != null) {
				 read2List.add(scaletab.getRead2());
			 }
		 }

		 // 添加条件：id 不在 read2 列表中
		 if (!read2List.isEmpty()) {
			 criteria.andIdNotIn(read2List);
		 }


	        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
	        List<Detection> list= detectionMapper.selectByExample(testExample);
	        PageInfo<Detection> pageInfo = new PageInfo<Detection>(list);
	        return  pageInfo;
	 }

	public PageInfo<Detection> list(Tablepar tablepar,Detection detection){

		DetectionExample testExample=new DetectionExample();
		testExample.createCriteria()
				.andVerifierIsNull()
				.andVerdictIsNull();
		//搜索
		if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
			testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
		}else {//大搜索
			testExample.createCriteria().andLikeQuery(detection);
		}
		//testExample.createCriteria().addCriterion("id NOT IN (SELECT id FROM table1)");
		//表格排序
		if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		}else{
			testExample.setOrderByClause("createTime desc");
		}


		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<Detection> list= detectionMapper.selectByExample(testExample);
		PageInfo<Detection> pageInfo = new PageInfo<Detection>(list);
		return  pageInfo;
	}
	public PageInfo<Detection> listShenhe(Tablepar tablepar,Detection detection){

		DetectionExample testExample = new DetectionExample();
		DetectionExample.Criteria criteria = testExample.createCriteria();

		// 搜索
		if (StrUtil.isNotEmpty(tablepar.getSearchText())) { // 小窗体
			criteria.andLikeQuery2(tablepar.getSearchText());
		} else { // 大搜索
			criteria.andLikeQuery(detection);
		}

		// 添加 verifier is null 条件
		criteria.andVerifierIsNull();

		// 查询 t_scaletab 表中的 read2 值列表
		ScaletabExample scaletabExample = new ScaletabExample();
		List<Scaletab> scaletabs = scaletabMapper.selectByExample(scaletabExample);
		java.util.List<String> read2List = new java.util.ArrayList<>();
		for (Scaletab scaletab : scaletabs) {
			if (scaletab.getRead2() != null) {
				read2List.add(scaletab.getRead2());
			}
		}

		// 添加 id in (select read2 from t_scaletab) 条件
		if (!read2List.isEmpty()) {
			criteria.andIdInStr(read2List);
		}

		// 表格排序
		if (StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) + " " + tablepar.getIsAsc());
		} else {
			testExample.setOrderByClause("createTime desc");
		}

		// 分页查询
		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<Detection> list = detectionMapper.selectByExample(testExample);
		PageInfo<Detection> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}
	public PageInfo<Detection> listPrint(Tablepar tablepar,Detection detection){

		DetectionExample testExample = new DetectionExample();
		DetectionExample.Criteria criteria = testExample.createCriteria();

		// 搜索
		if (StrUtil.isNotEmpty(tablepar.getSearchText())) { // 小窗体
			criteria.andLikeQuery2(tablepar.getSearchText());
		} else { // 大搜索
			criteria.andLikeQuery(detection);
		}
		// 添加 verifier is null 条件
		criteria.andVerifierIsNotNull();

		// 表格排序
		if (StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) + " " + tablepar.getIsAsc());
		} else {
			testExample.setOrderByClause("createTime desc");
		}

		// 分页查询
		PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
		List<Detection> list = detectionMapper.selectByExample(testExample);
		PageInfo<Detection> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}
	@Override
	public int deleteByPrimaryKey(String ids) {
			
		    List<String> list=ConvertUtil.toListStrArray(ids);
			DetectionExample example=new DetectionExample();
			example.createCriteria().andIdInStr(list);
			return detectionMapper.deleteByExample(example);
			
		
	}
	
	
	@Override
	public Detection selectByPrimaryKey(String id) {
		
			return detectionMapper.selectByPrimaryKey(id);
		
	}

	
	@Override
	public int updateByPrimaryKeySelective(Detection record) {
		return detectionMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(Detection record) {

		record.setId(SnowflakeIdWorker.getUUID());
		record.setCreatetime(new Date());
		record.setUpdatetime(new Date());
		return detectionMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(Detection record, DetectionExample example) {
		
		return detectionMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(Detection record, DetectionExample example) {
		
		return detectionMapper.updateByExample(record, example);
	}

	@Override
	public List<Detection> selectByExample(DetectionExample example) {
		
		return detectionMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(DetectionExample example) {
		
		return detectionMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(DetectionExample example) {
		
		return detectionMapper.deleteByExample(example);
	}
	
	/**
	 * 修改权限状态展示或者不展示
	 * @param detection
	 * @return
	 */
	public int updateVisible(Detection detection) {
		return detectionMapper.updateByPrimaryKeySelective(detection);
	}


}
